package com.onabilityresult;


import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.utils.Sequenceable;
import ohos.utils.net.Uri;

import java.io.Serializable;

public final class IntentHelper {
    
    public static Uri getIntentData(final Intent intent) {
        return intent != null ? intent.getUri() : null;
    }

    public static String getExtraString(final Intent intent, final String key, final String defaultValue) {
        final String extra = intent.getStringParam(key);
        return extra != null ? extra : defaultValue;
    }

    public static int getExtraInt(final Intent intent, final String key, final int defaultValue) {
        return intent.getIntParam(key, defaultValue);
    }

    public static boolean getExtraBoolean(final Intent intent, final String key, final boolean defaultValue) {
        return intent.getBooleanParam(key, defaultValue);
    }

    public static float getExtraFloat(final Intent intent, final String key, final float defaultValue) {
        return intent.getFloatParam(key, defaultValue);
    }

    public static double getExtraDouble(final Intent intent, final String key, final double defaultValue) {
        return intent.getDoubleParam(key, defaultValue);
    }

    public static byte getExtraByte(final Intent intent, final String key, final byte defaultValue) {
        return intent.getByteParam(key, defaultValue);
    }

    public static short getExtraShort(final Intent intent, final String key, final short defaultValue) {
        return intent.getShortParam(key, defaultValue);
    }

    public static char getExtraChar(final Intent intent, final String key, final char defaultValue) {
        return intent.getCharParam(key, defaultValue);
    }

    public static long getExtraLong(final Intent intent, final String key, final long defaultValue) {
        return intent.getLongParam(key, defaultValue);
    }

    public static CharSequence getExtraCharSequence(final Intent intent, final String key, final CharSequence defaultValue) {
        final CharSequence extra = intent.getCharSequenceParam(key);
        return extra != null ? extra : defaultValue;
    }

    public static IntentParams getExtraBundle(final Intent intent, final String key, final IntentParams defaultValue) {
        final IntentParams extra = intent.getParam(key);
        return extra != null ? extra : defaultValue;
    }

    public static <T extends Serializable> T getExtraSerializable(final Intent intent, final String key, final T defaultValue) {
        //noinspection unchecked
        final T extra = (T) intent.getSerializableParam(key);
        return extra != null ? extra : defaultValue;
    }

    public static <T extends Sequenceable> T getExtraParcelable(final Intent intent, final String key, final T defaultValue) {
        //noinspection unchecked
        final T extra = (T) intent.getSequenceableParam(key);
        return extra != null ? extra : defaultValue;
    }

    private static <T> T checkNotNull(final T t, final String message) {
        if (t == null) {
            throw new IllegalArgumentException(message);
        }

        return t;
    }

    private IntentHelper() {
        throw new AssertionError("No instances.");
    }
}
